<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #shopcar {
            padding: 10px;
            background-color: #fefefe;

        }

        .title {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .selet {
            font-size: 20px;
            font-weight: 700;
            line-height: 40px;
        }

        .item {
            display: flex;
            box-sizing: border-box;
            justify-content: space-between;
            align-items: center;
            background-color: aqua;
            padding: 10px;
            margin-bottom: 20px;
        }

        .item img {
            width: 50px;
        }

        .desc {
            color: red;
        }

        .left {
            display: flex;
        }

        .name {
            margin-bottom: 30px;
        }

        .right {
            padding-top: 40px;
        }

        .right span {
            width: 20px;
            height: 20px;
            border-radius: 50%;
            display: inline-block;
            background-color: #fefefe;
            text-align: center;
            line-height: 20px;
        }

        .foot {
            display: flex;
            box-sizing: border-box;
            justify-content: space-between;
            align-items: center;
            height: 80px;
            background-color: #474747;
        }

        .jiesuan {
            width: 100px;
            height: 40px;
            background-color: #02d562;
            color: #fefefe;
            border-radius: 20px;
            text-align: center;
            line-height: 40px;
        }

        .price {
            display: flex;
            color: #fefefe;
        }

        .jia {
            background-color: #00a6ff;
        }
    </style>
</head>

<body>
    <div id="shopcar">
        <div class="title">
            <span class="selet">已选商品</span>
            <span @click="qingkong()">清空</span>
        </div>
        <div class="list">
            <div class="item" v-for="(item,index) in carlist" v-bind:key="index">
                <div class="left">
                    <img v-bind:src="item.pic" alt="">
                    <div class="center">
                        <div class="name">{{item.name}}</div>
                        <div class="price">{{item.price}}</div>
                    </div>
                </div>
                <div class="right">
                    <span @click="reduce(index)">-</span>
                    <span>{{item.count}}</span>
                    <span @click="add(index)">+</span>
                </div>
            </div>
        </div>
        <div class="foot">
            <div class="price">
                <img src="./images/4.gif" alt="">
                <div>
                    <div>{{price}}</div>
                    <div>共计：{{sum}}件</div>
                    <div>另需配送费35元</div>
                </div>
            </div>
            <div class="jiesuan">去结算</div>
        </div>
    </div>
</body>

</html>
<script src="./vue-2.5.21.js"></script>
<script>
    var e = new Vue({
        el: '#shopcar',
        data: {
            carlist: [
                { id: 1, pic: './images/01.gif', name: '秋刀鱼', price: 35, count: 1 },
                { id: 2, pic: './images/02.gif', name: '鲜大带鱼/条', price: 45, count: 1 },
                { id: 1, pic: './images/3.gif', name: '鲜活全母皮皮虾/斤', price: 85, count: 1 },
            ]
        },
        methods: {
            reduce(index) {
                if (this.carlist[index].count > 1) {
                    this.carlist[index].count -= 1
                }
            },
            add(index) {
                if (this.carlist[index].count < 5) {
                    this.carlist[index].count += 1
                }
            },
            qingkong() {
                this.carlist = []
            }
        },
        computed: {
            price() {
                var zong = 0
                this.carlist.forEach((item) => {
                    zong += item.price * item.count
                })
                return zong
            },
            sum() {
                var number = 0
                this.carlist.forEach(item => {
                    number += item.count
                })
                return number
            }
        }, //computed 定义计算属性
        watch: {}, //watch 定义数据监听
        methods: {}, //methods 定义方法(函数)
        filters: {}, //filters 定义过滤器
        directives: {}, //directives 定义自定义指令
        components: {}, //components 注册局部组件
    })
</script>